package com.allen.retrofit2_rxjava2.view.selectImage.adapter;

import android.content.Context;
import android.graphics.Point;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;

import com.allen.retrofit2_rxjava2.R;
import com.allen.retrofit2_rxjava2.view.selectImage.entity.ChooseUploadParam;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by lierdong on 2016/11/29.
 */
public class UploadVideoImageAdapter extends RecyclerView.Adapter<UploadVideoImageAdapter.BaseViewHolder> {

    private static final String TAG = "UploadVideoImageAdapter";

    public static final int TYPE_ADD = 0;
    public static final int TYPE_VIDEO = 1;
    public static final int TYPE_IMG = 2;

    private Context context;
    private ArrayList<ChooseUploadParam> params;

    private OnItemClickListener onItemClickListener;

    public UploadVideoImageAdapter(Context context, ArrayList<ChooseUploadParam> params) {
        this.context = context;
        this.params = params;
    }

    public ArrayList<ChooseUploadParam> getParams() {
        return params;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView;
        BaseViewHolder holder = null;
        switch (viewType) {
            case TYPE_ADD://添加
                itemView = View.inflate(context, R.layout.item_add, null);
                holder = new AddItemViewHolder(itemView);
                break;
            case TYPE_VIDEO://播放
                itemView = View.inflate(context, R.layout.item_video, null);
                holder = new VideoItemViewHolder(itemView);
                break;
            case TYPE_IMG:
                itemView = View.inflate(context, R.layout.item_img, null);
                holder = new ImgItemViewHolder(itemView);
                break;
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
        ChooseUploadParam param = params.get(position);
        int viewType = getItemViewType(position);
        switch (viewType) {
//            case TYPE_ADD:
//
//                break;
            case TYPE_VIDEO:
                VideoItemViewHolder videoItemViewHolder = (VideoItemViewHolder) holder;
                String videoPath = param.getPath();
                String suffix = videoPath.substring(videoPath.lastIndexOf("."));
                String thumbnailPath = videoPath.replace(suffix, ".jpg");
                Glide.with(context.getApplicationContext())
                        .load(thumbnailPath)
                        .apply(new RequestOptions()
                                .diskCacheStrategy(DiskCacheStrategy.NONE)
                                .skipMemoryCache(true)
                                .error(R.mipmap.banner_default))
                        .into(videoItemViewHolder.iv_video);
                break;
            case TYPE_IMG:
                ImgItemViewHolder imgItemViewHolder = (ImgItemViewHolder) holder;
                String imgPath = param.getPath();
                Glide.with(context.getApplicationContext())
                        .load(imgPath)
                        .apply(new RequestOptions()
                                .diskCacheStrategy(DiskCacheStrategy.NONE)
                                .skipMemoryCache(true)
                                .error(R.mipmap.banner_default))
                        .into(imgItemViewHolder.iv_img);
                break;
        }
    }

    @Override
    public int getItemCount() {
        return params.size();
    }

    @Override
    public int getItemViewType(int position) {
        return params.get(position).getType();
    }

    private int correctHeight() {
        Point point = new Point();
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        windowManager.getDefaultDisplay().getSize(point);
        int margin = context.getResources().getDimensionPixelSize(R.dimen.margin);
        int itemDecoration = context.getResources().getDimensionPixelSize(R.dimen.itemDecoration);
        return (point.x - 2 * margin - 2 * itemDecoration) / 3;
    }

    class BaseViewHolder extends RecyclerView.ViewHolder {

        public View itemView;

        public BaseViewHolder(View itemView) {
            super(itemView);
            this.itemView = itemView;
        }
    }

    class AddItemViewHolder extends BaseViewHolder {

        public AddItemViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(new MyItemClickListener(TYPE_ADD, this));
        }
    }

    //视频长安监听
    class VideoItemViewHolder extends BaseViewHolder implements View.OnLongClickListener {

        public ImageView iv_video;

        public VideoItemViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(new MyItemClickListener(TYPE_VIDEO, this));
            iv_video = (ImageView) itemView.findViewById(R.id.iv_video);
            itemView.setOnLongClickListener(this);
        }

        @Override
        public boolean onLongClick(View v) {
            removeItem(getLayoutPosition());
            return true;
        }
    }

    //图片长安监听
    class ImgItemViewHolder extends BaseViewHolder implements View.OnLongClickListener {

        public ImageView iv_img;

        public ImgItemViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(new MyItemClickListener(TYPE_IMG, this));
            iv_img = (ImageView) itemView.findViewById(R.id.iv_img);
            itemView.setOnLongClickListener(this);
        }

        @Override
        public boolean onLongClick(View v) {
            removeItem(getLayoutPosition());
            return true;
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position, int viewType, ChooseUploadParam param);
    }

    class MyItemClickListener implements View.OnClickListener {

        private BaseViewHolder holder;
        private int viewType;

        public MyItemClickListener(int viewType, BaseViewHolder holder) {
            this.viewType = viewType;
            this.holder = holder;
        }

        @Override
        public void onClick(View v) {
            int position = holder.getLayoutPosition();
            if (onItemClickListener != null) {
                onItemClickListener.onItemClick(position, viewType, params.get(position));
            }
        }
    }

    private void removeParam(ChooseUploadParam param) {
        Iterator<ChooseUploadParam> iterator = params.iterator();
        while (iterator.hasNext()) {
            if (param.getPath().equals(iterator.next().getPath())) {
                iterator.remove();
            }
        }
    }

    private void removeItem(int position) {
        params.remove(position);
        if (params.get(getItemCount() - 1).getType() != TYPE_ADD) {
            ChooseUploadParam param = new ChooseUploadParam();
            param.setType(TYPE_ADD);
            params.add(param);
        }
        notifyItemRemoved(position);
    }

    public void addItem(ChooseUploadParam param) {
        switch (param.getType()) {
            case TYPE_VIDEO:
                if (hasVideo()) {
                    Toast.makeText(context, "只允许上传一个视频文件!", Toast.LENGTH_LONG).show();
                } else {
                    int position = getItemCount() - 1;
                    if (getItemCount() >= 9) {
                        params.set(position, param);
                        notifyItemChanged(position);
                    } else {
                        params.add(position, param);
                        notifyItemInserted(position);
                    }
                }
                break;
            case TYPE_IMG:
                int position = getItemCount() - 1;
                if (getItemCount() >= 9) {
                    params.set(position, param);
                    notifyItemChanged(position);
                } else {
                    params.add(position, param);
                    notifyItemInserted(position);
                }
                break;
        }
    }

    public void addItems(List<ChooseUploadParam> params) {
        for (ChooseUploadParam param : params) {
            addItem(param);
        }
    }

    public boolean hasVideo() {
        for (ChooseUploadParam param : params) {
            if (param.getType() == TYPE_VIDEO) {
                return true;
            }
        }
        return false;
    }

    public int getVideoImgCount() {
        int count = 0;
        for (ChooseUploadParam param : params) {
            if (param.getType() == TYPE_IMG || param.getType() == TYPE_VIDEO) {
                count++;
            }
        }
        return count;
    }

    public ArrayList<ChooseUploadParam> getVideoImgParams() {
        ArrayList<ChooseUploadParam> params = new ArrayList<>();
        for (ChooseUploadParam param : this.params) {
            if (param.getType() == TYPE_IMG || param.getType() == TYPE_VIDEO) {
                params.add(param);
            }
        }
        return params;
    }
}
